import { createRouter, createWebHistory } from 'vue-router';
// @ts-nocheck  
import Home from '../views/PC/Home.vue';
// @ts-nocheck  
import PcIndex from '../views/PC/index.vue';
// @ts-nocheck  
import Products from '../views/PC/Products.vue';
// @ts-nocheck  
import Resources from '../views/PC/Resources.vue';
// @ts-nocheck  
import Company from '../views/PC/Company.vue';
// @ts-nocheck  
import Connect from '../views/PC/Connect.vue';
// @ts-nocheck  
import PcAbout from '../views/PC/About.vue';

// @ts-nocheck  
import adminHome from '../views/Admin/Home.vue';
// @ts-nocheck  
import adminIndex from '../views/Admin/index.vue';
// @ts-nocheck  
import adminLibrariesIndex from '../views/Admin/Libraries/index.vue';
// @ts-nocheck  
import adminLibrariesCreate from '../views/Admin/Libraries/Create.vue';
// @ts-nocheck  
import adminLibrariesEdit from '../views/Admin/Libraries/Edit.vue';
// @ts-nocheck  
import adminSetting from '../views/Admin/Setting.vue';

// @ts-nocheck  
import store from '../store/index.ts'; // 引入Vuex store  

const routes = [
  {
    path: "/",
    name: "index",
    redirect: "/index",
  },
  {
    path: "/home",
    name: "index",
    redirect: "/index",
  },
  {
    path: "/index",
    name: "index",
    redirect: "/index",
  },
  {
    path: '/',
    name: "index",
    component: Home,
    redirect: "/index",
    meta: {
      title: '首页'
    },
    children: [
      {
        path: '/index',
        name: 'index',
        component: PcIndex,
      },
      {
        path: '/about',
        name: 'About',
        component: PcAbout,
      },
      {
        path: '/Products',
        name: 'Products',
        component: Products,
      },
      {
        path: '/Resources',
        name: 'Resources',
        component: Resources,
      },
      {
        path: '/Company',
        name: 'Company',
        component: Company,
      },
      {
        path: '/Connect',
        name: 'Connect',
        component: Connect,
      },
    ]
  },
  {
    path: "/admin/home",
    component: adminHome,
    meta: {
      title: "自述文件",
    },
    children: [
      {
        path: "/admin/index",
        component: adminIndex,
        meta: {
          title: "首页",
        },
      },
      {
        path: "/admin/libraries/index",
        component: adminLibrariesIndex,
        meta: {
          title: "数据管理",
        },
      },
      {
        path: "/admin/libraries/create",
        component: adminLibrariesCreate,
        meta: {
          title: "数据创建",
        },
      },
      {
        path: "/admin/libraries/edit/:id",
        component: adminLibrariesEdit,
        meta: {
          title: "数据编辑",
        },
      },
      {
        path: "/admin/setting",
        component: adminSetting,
        meta: {
          title: "设置",
        },
      },
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});
// router.beforeEach中的代码  
// @ts-nocheck  
router.beforeEach((to, from, next) => {
  console.log(from)
  // 检查新路由是否已经在标签页数组中  
  // @ts-nocheck  
  const existingTag = store.state.tags.find(tag => tag.path === to.path);

  // @ts-nocheck  
  store.state.tags.forEach(tag => {
    tag.active = false;
  });
  // 如果标签不存在，则添加新标签  
  if (!existingTag) {
    store.dispatch('addTag', {
      path: to.path,
      title: to.meta.title || to.name || to.path, // 使用meta中的title，如果没有则使用name或path  
      active: true
    });
  }
  else {
    existingTag.active = true;
  }

  next();
});

export default router;